package cn.mw.cmdb.template.strategy.handler;

import cn.mw.cmdb.exception.ZabbixResultException;
import cn.mw.cmdb.template.constant.MwTemplateConfigurationConstant;
import cn.mw.cmdb.template.enums.TemplateDetailEnum;
import cn.mw.cmdb.template.param.HostParam;
import cn.mw.cmdb.template.param.MwTemplateConfigurationParam;
import cn.mw.cmdb.template.strategy.TemplateDetailHandler;
import cn.mw.cmdb.template.utils.TemplateSortUtil;
import cn.mw.microMonitorCommon.constants.ZabbixApiConstants;
import cn.mw.microMonitorCommon.constants.ZabbixMethodConstants;
import cn.mw.microMonitorCommon.entity.zabbix.MWZabbixAPIResult;
import cn.mw.microMonitorCommon.util.ListMapObjUtils;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description 主机处理
 */
@Service
@Slf4j
public class HostHandler extends TemplateDetailHandler {

    @Override
    public TemplateDetailEnum handlerType() {
        return TemplateDetailEnum.HOST;
    }


    @Override
    public Object getData(MwTemplateConfigurationParam param) {
        //获取模板下的主机信息
        Map zabbixParam = new HashMap();
        zabbixParam.put(MwTemplateConfigurationConstant.TEMPLATE_IDS, param.getTemplateId());
        zabbixParam.put(MwTemplateConfigurationConstant.OUTPUT, MwTemplateConfigurationConstant.EXTEND);
        zabbixParam.put(MwTemplateConfigurationConstant.selectDiscoveries, MwTemplateConfigurationConstant.COUNT);
        zabbixParam.put(MwTemplateConfigurationConstant.selectItems, MwTemplateConfigurationConstant.COUNT);
        zabbixParam.put(MwTemplateConfigurationConstant.selectTriggers, MwTemplateConfigurationConstant.COUNT);
        zabbixParam.put(MwTemplateConfigurationConstant.selectInterfaces, MwTemplateConfigurationConstant.EXTEND);
        zabbixParam.put(MwTemplateConfigurationConstant.selectTags, MwTemplateConfigurationConstant.EXTEND);
        zabbixParam.put(MwTemplateConfigurationConstant.selectParentTemplates, MwTemplateConfigurationConstant.EXTEND);
        MWZabbixAPIResult zabbixAPIResult = zabbixDubboService.getZabbixApiResult(ZabbixApiConstants.GET_CUSTOM_INFO_BY_METHOD, param.getServerId(), ZabbixMethodConstants.HOST_GET, zabbixParam);
        if(zabbixAPIResult == null || zabbixAPIResult.isFail()){
            throw new ZabbixResultException(String.valueOf(zabbixAPIResult.getData()));
        }
        List<HostParam> hostParams = JSONArray.parseArray(String.valueOf(zabbixAPIResult.getData()), HostParam.class);
        //数据排序
        List realData = TemplateSortUtil.templateSortList(hostParams, param,param.getQuery());
        if(param.getPageNum() == null || param.getPageSize() == null){
            return realData;
        }
        Page page = pageHandler(realData, param.getPageNum(), param.getPageSize());
        return page;
    }

    @Override
    public Object createData(MwTemplateConfigurationParam param) {

        return null;
    }

    @Override
    public Object updateData(MwTemplateConfigurationParam param) {

        return null;
    }

    @Override
    public Object deleteData(MwTemplateConfigurationParam param) {

        return null;
    }

    @Override
    public Object selectDataById(MwTemplateConfigurationParam param) {
        Map zabbixParam = new HashMap();
        zabbixParam.put(HOST_IDS, Arrays.asList(param.getDetailId()));
        zabbixParam.put(MwTemplateConfigurationConstant.OUTPUT, MwTemplateConfigurationConstant.EXTEND);
        zabbixParam.put(MwTemplateConfigurationConstant.selectGroups, MwTemplateConfigurationConstant.EXTEND);
        zabbixParam.put(MwTemplateConfigurationConstant.selectInterfaces, MwTemplateConfigurationConstant.EXTEND);
        zabbixParam.put(MwTemplateConfigurationConstant.selectInventory, MwTemplateConfigurationConstant.EXTEND);
        zabbixParam.put(MwTemplateConfigurationConstant.selectMacros, MwTemplateConfigurationConstant.EXTEND);
        zabbixParam.put(MwTemplateConfigurationConstant.selectParentTemplates, MwTemplateConfigurationConstant.EXTEND);
        zabbixParam.put(MwTemplateConfigurationConstant.selectTags, MwTemplateConfigurationConstant.EXTEND);
        MWZabbixAPIResult zabbixAPIResult = zabbixDubboService.getZabbixApiResult(ZabbixApiConstants.GET_CUSTOM_INFO_BY_METHOD, param.getServerId(), ZabbixMethodConstants.HOST_GET, zabbixParam);
        if(zabbixAPIResult == null || zabbixAPIResult.isFail()){
            throw new ZabbixResultException(String.valueOf(zabbixAPIResult.getData()));
        }
        List<HostParam> hostParams = JSONArray.parseArray(String.valueOf(zabbixAPIResult.getData()), HostParam.class);
        if(CollectionUtils.isEmpty(hostParams)){return null;}
        return hostParams.get(0);
    }


}
